import Vue from 'vue'
import App from './App.vue'
import router from './config/router'
import './plugins/element.js'
import axios from 'axios'
import api from './config/api.js'
import httpf from './util/httpUtil.js'
import qs from 'qs'
import './components/globel'
// 导入 NProgress 包对应的JS和CSS
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.prototype.$qs = qs
Vue.prototype.$api = api
Vue.prototype.$http = axios
Vue.prototype.$httpf = httpf
Vue.config.productionTip = false

axios.interceptors.request.use(config => {
  NProgress.start()
  // config.headers.token = window.sessionStorage.getItem('token')
  // 在最后必须 return config
  return config
})

// 在 response 拦截器中，隐藏进度条 NProgress.done()
axios.interceptors.response.use(res => {
  NProgress.done()
  return res
// eslint-disable-next-line handle-callback-err
}, function (err, data) {
  NProgress.done()
  try {
    if (err.response.data.errors[0].defaultMessage) {
      vm.$error(err.response.data.errors[0].defaultMessage)
      return
    }
  } catch (e) {
    console.error(e)
  }
  if (err.response.data.status && err.response.data.message) {
    vm.$error(err.response.data.message)
  }
})

const vm = new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
